package com.gitee.dome1;

import java.util.ArrayList;
import java.util.Scanner;
//这是一个主main方法。
public class Demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();

        lo:while (true) {
            System.out.println("---------欢迎来到学生管理系统-------");
            System.out.println("1添加学生");
            System.out.println("2删除学生");
            System.out.println("3修改学生");
            System.out.println("4查看学生");
            System.out.println("5退出");
            System.out.println("请输入您的选择");
            String ss = sc.next();
            switch (ss) {
                case "1":
                    System.out.println("添加学生");
                    addStudent(list);
                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case "4":
                    System.out.println("查看学生");
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("谢谢您的使用");
                    break lo ;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }
    //修改学生信息
    public static void updateStudent(ArrayList<Student>list) {
        System.out.println("请输入您要修改的学号");
        Scanner sc = new Scanner(System.in);
        String updatesid = sc.next();
        int index = getIndex(list,updatesid);
        if (index == -1){
            System.out.println("查无信息，请重新输入");
        }else{
            System.out.println("请输入新的姓名");
            String name = sc.next();
            System.out.println("请输入新的年龄");
            int age = sc.nextInt();
            System.out.println("请输入新的学生生日");
            String birthday = sc.next();
            Student stu = new Student(updatesid,name,age,birthday);
            list.set(index,stu);
            System.out.println("修改成功");

        }
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入您要删除的学生学号");
        Scanner sc = new Scanner(System.in);
        String deletesid = sc.next();
        int index = getIndex(list,deletesid);
        if (index == -1){
            System.out.println("查无信息，请重新输入");
        }else{
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    //查看学生
    private static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0){
            System.out.println("无信息，请添加后重新查询");
            return;
        }
        System.out.println("学号  姓名  年龄  生日");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid()+"  "+stu.getName()+"  "+stu.getAge()+"  "+ stu.getBirthday());
        }
    }

    //添加学生方法
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            int index = getIndex(list,sid);
            if (index == -1){
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student stu = new Student(sid,name,age,birthday);
        list.add(stu);
        System.out.println("添加成功");
    }
    //判断学号是否存在
    public static  int getIndex(ArrayList<Student>list,String sid){
        int jiashe = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String id = stu.getSid();
            if (id.equals(sid)){
                jiashe = i;
            }
        }
        return jiashe;
    }
}
